package com.ruoyi.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.UUIDgen;
import com.ruoyi.common.security.utils.DictUtils;
import com.ruoyi.mobile.domain.*;
import com.ruoyi.mobile.mapper.*;
import com.ruoyi.mobile.service.IShOrderService;
import com.ruoyi.mobile.service.MaterialjkService;
import com.ruoyi.mobile.utils.NftResponse;
import com.ruoyi.mobile.utils.Paginator;
import com.ruoyi.mobile.utils.QueryUtil;
import com.ruoyi.mobile.utils.ResponseConstants;
import com.ruoyi.mobile.utils.baiduT.BaiduTranslator;
import com.ruoyi.mobile.utils.baiduV.BaiduVoice;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.req.*;
import com.ruoyi.system.api.resp.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class MaterialjkServiceImpl extends ServiceImpl<MaterialInfojkMapper, MaterialInfojkEntity> implements MaterialjkService {
    @Autowired
    private IShOrderService shOrderService;
    @Autowired
    private MaterialjkOrderMapper orderjkMapper;
    @Autowired
    private MaterialjkCollectMapper collectjkMapper;
    @Autowired
    private MaterialjkViewMapper viewjkMapper;
    @Autowired
    private ISysUserMapper sysUserMapper;
    @Autowired
    private MaterialInfojkMapper materialInfojkMapper;
    @Autowired
    private IShBannerMapper bannerMapper;
    @Autowired
    private IShCourseTeacherMapper teacherMapper;

    @Override
    public NftResponse<Paginator<MaterialInfojkResp>> getMaterialjkList(MaterialInfojkReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        List<SysDictData> dictGList = DictUtils.getDictCache("grade");
        List<SysDictData> dictSList = DictUtils.getDictCache("subject");
        List<SysDictData> dictMList = DictUtils.getDictCache("semester");
        HashMap<String, String> dictGMap = new HashMap<>();
        HashMap<String, String> dictSMap = new HashMap<>();
        HashMap<String, String> dictMMap = new HashMap<>();
        for (SysDictData data:dictGList) {
            dictGMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictSList) {
            dictSMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictMList) {
            dictMMap.put(data.getDictValue(),data.getDictLabel());
        }
        Paginator<MaterialInfojkResp> paginator = new Paginator<MaterialInfojkResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<MaterialInfojkEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=query.getGrade()&&query.getGrade()>0) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("grade"), query.getGrade());
        }
        if (null!=query.getSubject()&&query.getSubject()>0) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("subject"), query.getSubject());
        }
        if (null!=query.getSemester()&&query.getSemester()>0) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("semester"), query.getSemester());
        }
        if (null!=query.getAge()&&query.getAge()>0) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("age"), query.getAge());
        }
        if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("appId"),query.getAppid());
        }
        if (null!=query.getCollection()&&!"".equals(query.getCollection())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("collection"), query.getCollection());
        }
        if (null!=query.getVersion()&&!"".equals(query.getVersion())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("version"), query.getVersion());
        }
        if (null!=query.getMaterialName()&&!"".equals(query.getMaterialName())) {
            wrapperQuery.like(QueryUtil.camelToUnderline("materialName"), query.getMaterialName());
        }
        if (null!=query.getIsHold()&&query.getIsHold()==1&&null!=query.getUserId()) {
            wrapperQuery.inSql("id", "select material_id from materialjk_view where is_hold=1 and user_id="+query.getUserId());
        }
        if (null!=query.getIsHold()&&query.getIsHold()==0&&null!=query.getUserId()) {
            wrapperQuery.inSql("id", "select material_id from materialjk_view where is_hold=0 and user_id="+query.getUserId());
        }
        if ("1".equals(query.getPxType())) {
            wrapperQuery.orderByDesc("create_time");
        }
        IPage<MaterialInfojkEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<MaterialInfojkEntity> pageRtn = this.baseMapper.selectPage(page,wrapperQuery);
        List<MaterialInfojkEntity> list = pageRtn.getRecords();
        List<MaterialInfojkResp> result = BeanUtil.copyToList(list, MaterialInfojkResp.class);
        for (MaterialInfojkResp obj:result) {
            obj.setGradeName(dictGMap.get(obj.getGrade()+""));
            obj.setSubjectName(dictSMap.get(obj.getSubject()+""));
            obj.setSemesterName(dictMMap.get(obj.getSemester()+""));
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<Paginator<MaterialjkOrderResp>> getOrderList(MaterialjkOrderReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        List<SysDictData> dictGList = DictUtils.getDictCache("grade");
        List<SysDictData> dictSList = DictUtils.getDictCache("subject");
        List<SysDictData> dictMList = DictUtils.getDictCache("semester");
        HashMap<String, String> dictGMap = new HashMap<>();
        HashMap<String, String> dictSMap = new HashMap<>();
        HashMap<String, String> dictMMap = new HashMap<>();
        for (SysDictData data:dictGList) {
            dictGMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictSList) {
            dictSMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictMList) {
            dictMMap.put(data.getDictValue(),data.getDictLabel());
        }
        Paginator<MaterialjkOrderResp> paginator = new Paginator<MaterialjkOrderResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<MaterialjkOrderEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=query.getUserId()&&query.getUserId()>0) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("userId"), query.getUserId());
        }
        if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
            wrapperQuery.inSql("material_id", "select id from material_infojk where app_id='"+query.getAppid()+"'");
        }
        IPage<MaterialjkOrderEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<MaterialjkOrderEntity> pageRtn = orderjkMapper.selectPage(page,wrapperQuery);
        List<MaterialjkOrderEntity> list = pageRtn.getRecords();
        List<MaterialjkOrderResp> result = BeanUtil.copyToList(list, MaterialjkOrderResp.class);
        for (MaterialjkOrderResp obj:result) {
            MaterialInfojkEntity entity = this.baseMapper.selectById(obj.getMaterialId());
            if (null!=entity) {
                obj.setVersionD((entity.getAge()==0?"":entity.getAge()) +
                        (entity.getGrade()==0?"":dictGMap.get(entity.getGrade() + "")) +
                        (entity.getSemester()==0?"":dictMMap.get(entity.getSemester() + "")));
                obj.setVersion(entity.getVersion());
                obj.setMaterialName(entity.getMaterialName());
                obj.setMaterialKey(entity.getMaterialKey());
            }
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<Paginator<MaterialjkCollectResp>> getCollectList(MaterialjkCollectReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        List<SysDictData> dictGList = DictUtils.getDictCache("grade");
        List<SysDictData> dictSList = DictUtils.getDictCache("subject");
        List<SysDictData> dictMList = DictUtils.getDictCache("semester");
        HashMap<String, String> dictGMap = new HashMap<>();
        HashMap<String, String> dictSMap = new HashMap<>();
        HashMap<String, String> dictMMap = new HashMap<>();
        for (SysDictData data:dictGList) {
            dictGMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictSList) {
            dictSMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictMList) {
            dictMMap.put(data.getDictValue(),data.getDictLabel());
        }
        Paginator<MaterialjkCollectResp> paginator = new Paginator<MaterialjkCollectResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<MaterialjkCollectEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=query.getUserId()&&query.getUserId()>0) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("userId"), query.getUserId());
        }
        if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
            wrapperQuery.inSql("material_id", "select id from material_infojk where app_id='"+query.getAppid()+"'");
        }
        IPage<MaterialjkCollectEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<MaterialjkCollectEntity> pageRtn = collectjkMapper.selectPage(page,wrapperQuery);
        List<MaterialjkCollectEntity> list = pageRtn.getRecords();
        List<MaterialjkCollectResp> result = BeanUtil.copyToList(list, MaterialjkCollectResp.class);
        for (MaterialjkCollectResp obj:result) {
            MaterialInfojkEntity entity = this.baseMapper.selectById(obj.getMaterialId());
            if (null!=entity) {
                obj.setVersionD(entity.getAge()==0?"":entity.getAge() +
                        (entity.getGrade()==0?"":dictGMap.get(entity.getGrade() + "")) +
                        (entity.getSemester()==0?"":dictMMap.get(entity.getSemester() + "")));
                obj.setVersion(entity.getVersion());
                obj.setMaterialName(entity.getMaterialName());
                obj.setMaterialKey(entity.getMaterialKey());
            }
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<MaterialInfojkResp> getMaterialInfo(Long id, Long userId) {
        QueryWrapper<MaterialjkOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(QueryUtil.camelToUnderline("materialId"), id);
        wrapper.eq(QueryUtil.camelToUnderline("userId"), userId);
        wrapper.eq(QueryUtil.camelToUnderline("status"), 1);
        List<MaterialjkOrderEntity> orderList = orderjkMapper.selectList(wrapper);
        if (null==orderList||orderList.size()<=0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "未支付卡片不能查看详情", null);
        }
        List<SysDictData> dictGList = DictUtils.getDictCache("grade");
        List<SysDictData> dictSList = DictUtils.getDictCache("subject");
        List<SysDictData> dictMList = DictUtils.getDictCache("semester");
        HashMap<String, String> dictGMap = new HashMap<>();
        HashMap<String, String> dictSMap = new HashMap<>();
        HashMap<String, String> dictMMap = new HashMap<>();
        for (SysDictData data:dictGList) {
            dictGMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictSList) {
            dictSMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictMList) {
            dictMMap.put(data.getDictValue(),data.getDictLabel());
        }
        MaterialInfojkEntity entity = this.baseMapper.selectById(id);
        setContent(entity);
        MaterialInfojkResp rtn = BeanUtil.copyProperties(entity, MaterialInfojkResp.class);
        rtn.setGradeName(dictGMap.get(rtn.getGrade()+""));
        rtn.setSubjectName(dictSMap.get(rtn.getSubject()+""));
        rtn.setSemesterName(dictMMap.get(rtn.getSemester()+""));
        QueryWrapper<MaterialInfojkEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=rtn.getVersion()&&!"".equals(rtn.getVersion())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("version"), rtn.getVersion());
        }
        List<MaterialInfojkEntity> listJk = this.materialInfojkMapper.selectList(wrapperQuery);
        if (null!=listJk&&listJk.size()>0) {
            for (MaterialInfojkEntity entityJk:listJk) {
                setContent(entityJk);
            }
            rtn.setJkNum(listJk.size());
            List<MaterialInfojkResp> result = BeanUtil.copyToList(listJk, MaterialInfojkResp.class);
            rtn.setListJk(result);
        }
        QueryWrapper<MaterialjkViewEntity> wrapperZw = new QueryWrapper<>();
        wrapperZw.inSql("material_id", "select id from material_infojk where version='"+entity.getVersion()+"'");
        wrapperZw.eq(QueryUtil.camelToUnderline("user_id"), userId);
        wrapperZw.eq(QueryUtil.camelToUnderline("is_hold"), 1);
        List<MaterialjkViewEntity> zwList = viewjkMapper.selectList(wrapperZw);

        QueryWrapper<MaterialjkViewEntity> wrapperWZw = new QueryWrapper<>();
        wrapperWZw.inSql("material_id", "select id from material_infojk where version='"+entity.getVersion()+"'");
        wrapperWZw.eq(QueryUtil.camelToUnderline("user_id"), userId);
        wrapperWZw.eq(QueryUtil.camelToUnderline("is_hold"), 0);
        List<MaterialjkViewEntity> wzwList = viewjkMapper.selectList(wrapperWZw);
        if (null!=zwList&&zwList.size()>0) {
            rtn.setJkNumZw(zwList.size());
        }
        if (null!=wzwList&&wzwList.size()>0) {
            rtn.setJkNumWZw(wzwList.size());
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", rtn);
    }

    @Override
    public NftResponse<MaterialInfojkResp> getMaterialInfot(Long id, Long userId) {
        QueryWrapper<MaterialjkOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(QueryUtil.camelToUnderline("materialId"), id);
        wrapper.eq(QueryUtil.camelToUnderline("userId"), userId);
        wrapper.eq(QueryUtil.camelToUnderline("status"), 1);
        List<MaterialjkOrderEntity> orderList = orderjkMapper.selectList(wrapper);
        if (null==orderList||orderList.size()<=0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "未支付卡片不能查看详情", null);
        }
        List<SysDictData> dictGList = DictUtils.getDictCache("grade");
        List<SysDictData> dictSList = DictUtils.getDictCache("subject");
        List<SysDictData> dictMList = DictUtils.getDictCache("semester");
        HashMap<String, String> dictGMap = new HashMap<>();
        HashMap<String, String> dictSMap = new HashMap<>();
        HashMap<String, String> dictMMap = new HashMap<>();
        for (SysDictData data:dictGList) {
            dictGMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictSList) {
            dictSMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictMList) {
            dictMMap.put(data.getDictValue(),data.getDictLabel());
        }
        MaterialInfojkEntity entity = this.baseMapper.selectById(id);
        setContent(entity);
        MaterialInfojkResp rtn = BeanUtil.copyProperties(entity, MaterialInfojkResp.class);
        rtn.setGradeName(dictGMap.get(rtn.getGrade()+""));
        rtn.setSubjectName(dictSMap.get(rtn.getSubject()+""));
        rtn.setSemesterName(dictMMap.get(rtn.getSemester()+""));

        QueryWrapper<MaterialInfojkEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=rtn.getVersion()&&!"".equals(rtn.getVersion())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("version"), rtn.getVersion());
        }
        List<MaterialInfojkEntity> listJk = this.materialInfojkMapper.selectList(wrapperQuery);
        if (null!=listJk&&listJk.size()>0) {
            for (MaterialInfojkEntity entityJk:listJk) {
                setContent(entityJk);
            }
            rtn.setJkNum(listJk.size());
            List<MaterialInfojkResp> result = BeanUtil.copyToList(listJk, MaterialInfojkResp.class);
            rtn.setListJk(result);
        }
        QueryWrapper<MaterialjkViewEntity> wrapperZw = new QueryWrapper<>();
        wrapperZw.inSql("material_id", "select id from material_infojk where version='"+entity.getVersion()+"'");
        wrapperZw.eq(QueryUtil.camelToUnderline("user_id"), userId);
        wrapperZw.eq(QueryUtil.camelToUnderline("is_hold"), 1);
        List<MaterialjkViewEntity> zwList = viewjkMapper.selectList(wrapperZw);

        QueryWrapper<MaterialjkViewEntity> wrapperWZw = new QueryWrapper<>();
        wrapperWZw.inSql("material_id", "select id from material_infojk where version='"+entity.getVersion()+"'");
        wrapperWZw.eq(QueryUtil.camelToUnderline("user_id"), userId);
        wrapperWZw.eq(QueryUtil.camelToUnderline("is_hold"), 0);
        List<MaterialjkViewEntity> wzwList = viewjkMapper.selectList(wrapperWZw);
        if (null!=zwList&&zwList.size()>0) {
            rtn.setJkNumZw(zwList.size());
        }
        if (null!=wzwList&&wzwList.size()>0) {
            rtn.setJkNumWZw(wzwList.size());
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", rtn);
    }

    @Override
    public NftResponse<String> playVoice(Long id, Long userId) {
        QueryWrapper<MaterialjkOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(QueryUtil.camelToUnderline("materialId"), id);
        wrapper.eq(QueryUtil.camelToUnderline("userId"), userId);
        wrapper.eq(QueryUtil.camelToUnderline("status"), 1);
        List<MaterialjkOrderEntity> orderList = orderjkMapper.selectList(wrapper);
        if (null==orderList||orderList.size()<=0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "未支付卡片不能播放语音", null);
        }
        MaterialInfojkEntity entity = this.baseMapper.selectById(id);
        if (null==entity.getUrl()||"".equals(entity.getUrl())) {
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", getVoice(entity));
        }else {
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", entity.getUrl());
        }
    }

    private void setContent(MaterialInfojkEntity entity){
        if (null==entity.getMaterialNamet()||"".equals(entity.getMaterialNamet())) {
            try {
                String detail = BaiduTranslator.translate(entity.getMaterialName(), "en", "zh");
                entity.setMaterialNamet(detail);
                this.baseMapper.updateById(entity);
            }catch (Exception e) {e.printStackTrace();}
        }
    }
    private String getVoice(MaterialInfojkEntity entity){
        String url = null;
        if (null==entity.getUrl()||"".equals(entity.getUrl())) {
            try {
                url = BaiduVoice.getVoice(entity.getMaterialName(), entity.getId()+"voice");
                entity.setUrl(url);
                this.baseMapper.updateById(entity);
            }catch (Exception e) {e.printStackTrace();}
        }
        return url;
    }

    @Override
    public NftResponse<JSONObject> insertOrderInfo(MaterialjkOrderReq query) {
        if (null == query.getBannerId() || query.getBannerId() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入下单卡包", null);
        }
        ShBannerEntity entityB = this.bannerMapper.selectById(query.getBannerId());
        QueryWrapper<MaterialInfojkEntity> wrapperQuery = new QueryWrapper<>();
        wrapperQuery.eq(QueryUtil.camelToUnderline("version"), entityB.getProductTitle());
        List<MaterialInfojkEntity> listJk = this.materialInfojkMapper.selectList(wrapperQuery);
        BigDecimal amount = BigDecimal.valueOf(0);
        String orderNo = UUIDgen.getUUID(20);
        for (MaterialInfojkEntity entityJk:listJk) {
            QueryWrapper<MaterialjkOrderEntity> wrapper = new QueryWrapper<>();
            wrapper.eq(QueryUtil.camelToUnderline("materialId"), entityJk.getId());
            wrapper.eq(QueryUtil.camelToUnderline("userId"), query.getUserId());
            List<MaterialjkOrderEntity> orderList = orderjkMapper.selectList(wrapper);
            if (null==orderList||orderList.size()<=0) {
                SysUserEntity sysUser = sysUserMapper.selectById(query.getUserId());
                MaterialjkOrderEntity entity = BeanUtil.copyProperties(query, MaterialjkOrderEntity.class);
                entity.setOrderNo(orderNo);
                entity.setMaterialId(entityJk.getId());
                entity.setUsername(sysUser.getUserName());
                entity.setUserphone(sysUser.getPhonenumber());
                entity.setCreateTime(DateUtils.getNowDate());
                if (null==entityJk.getAmount() || entityJk.getAmount().compareTo(BigDecimal.ZERO)<=0) {
                    entity.setStatus(1);
                }
                orderjkMapper.insert(entity);
                if (null!=entityJk.getAmount()) {
                    amount.add(entityJk.getAmount());
                }
            }
        }
        if (null!=query.getAmount() && query.getAmount().compareTo(BigDecimal.ZERO)>0) {
            BigDecimal fee = amount.multiply(BigDecimal.valueOf(100));
            try {
                String appId = query.getAppid();
                NftResponse<JSONObject> rtn = shOrderService.wxPayFunction(appId, String.valueOf(fee.intValue()), orderNo, query.getUserId());
                return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "下单成功", rtn.getData());
            }catch (Exception e){}
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "下单成功", null);
    }

    @Override
    public NftResponse<JSONObject> insertOrderInfot(MaterialjkOrderReq query) {
        if (null == query.getTeacherId() || query.getTeacherId() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入下单教师卡包", null);
        }
        ShCourseTeacherEntity entityT = this.teacherMapper.selectById(query.getTeacherId());
        QueryWrapper<MaterialInfojkEntity> wrapperQuery = new QueryWrapper<>();
        wrapperQuery.eq(QueryUtil.camelToUnderline("version"), entityT.getTeacherName());
        List<MaterialInfojkEntity> listJk = this.materialInfojkMapper.selectList(wrapperQuery);
        BigDecimal amount = BigDecimal.valueOf(0);
        String orderNo = UUIDgen.getUUID(20);
        for (MaterialInfojkEntity entityJk:listJk) {
            QueryWrapper<MaterialjkOrderEntity> wrapper = new QueryWrapper<>();
            wrapper.eq(QueryUtil.camelToUnderline("materialId"), entityJk.getId());
            wrapper.eq(QueryUtil.camelToUnderline("userId"), query.getUserId());
            List<MaterialjkOrderEntity> orderList = orderjkMapper.selectList(wrapper);
            if (null==orderList||orderList.size()<=0) {
                SysUserEntity sysUser = sysUserMapper.selectById(query.getUserId());
                MaterialjkOrderEntity entity = BeanUtil.copyProperties(query, MaterialjkOrderEntity.class);
                entity.setOrderNo(orderNo);
                entity.setMaterialId(entityJk.getId());
                entity.setUsername(sysUser.getUserName());
                entity.setUserphone(sysUser.getPhonenumber());
                entity.setCreateTime(DateUtils.getNowDate());
                if (null==entityJk.getAmount() || entityJk.getAmount().compareTo(BigDecimal.ZERO)<=0) {
                    entity.setStatus(1);
                }
                orderjkMapper.insert(entity);
                if (null!=entityJk.getAmount()) {
                    amount.add(entityJk.getAmount());
                }
            }
        }
        if (null!=query.getAmount() && query.getAmount().compareTo(BigDecimal.ZERO)>0) {
            BigDecimal fee = amount.multiply(BigDecimal.valueOf(100));
            try {
                String appId = query.getAppid();
                NftResponse<JSONObject> rtn = shOrderService.wxPayFunction(appId, String.valueOf(fee.intValue()), orderNo, query.getUserId());
                return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "下单成功", rtn.getData());
            }catch (Exception e){}
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "下单成功", null);
    }

    @Override
    public NftResponse<JSONObject> insertCollectInfo(MaterialjkCollectReq query) {
        if (null == query.getMaterialId() || query.getMaterialId() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入下单句块", null);
        }
        QueryWrapper<MaterialjkCollectEntity> wrapperC = new QueryWrapper<>();
        wrapperC.eq(QueryUtil.camelToUnderline("material_id"), query.getMaterialId());
        wrapperC.eq(QueryUtil.camelToUnderline("user_id"), query.getUserId());
        List<MaterialjkCollectEntity> collectList = collectjkMapper.selectList(wrapperC);
        if (null!=collectList&&collectList.size()>0) {
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "重复收藏", null);
        }else {
            SysUserEntity sysUser = sysUserMapper.selectById(query.getUserId());
            MaterialjkCollectEntity entity = BeanUtil.copyProperties(query, MaterialjkCollectEntity.class);
            entity.setUsername(sysUser.getUserName());
            entity.setUserphone(sysUser.getPhonenumber());
            entity.setCreateTime(DateUtils.getNowDate());
            collectjkMapper.insert(entity);
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "收藏成功", null);
        }
    }

    @Override
    public NftResponse<JSONObject> insertHoldInfojk(MaterialjkViewReq query) {
        if (null == query.getMaterialId() || query.getMaterialId() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入下单句块", null);
        }
        QueryWrapper<MaterialjkViewEntity> wrapperV = new QueryWrapper<>();
        wrapperV.eq(QueryUtil.camelToUnderline("material_id"), query.getMaterialId());
        wrapperV.eq(QueryUtil.camelToUnderline("user_id"), query.getUserId());
        List<MaterialjkViewEntity> viewList = viewjkMapper.selectList(wrapperV);
        if (null==viewList||viewList.size()<=0) {
            MaterialjkViewEntity entity = BeanUtil.copyProperties(query, MaterialjkViewEntity.class);
            entity.setUserId(query.getUserId());
            entity.setIsHold(query.getIsHold());
            viewjkMapper.insert(entity);
        }else {
            MaterialjkViewEntity entity = viewList.get(0);
            entity.setUserId(query.getUserId());
            entity.setIsHold(query.getIsHold());
            viewjkMapper.updateById(entity);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "设置成功", null);
    }


    @Override
    public NftResponse<Boolean> delOrderjk(Long id) {
        int rtn = orderjkMapper.deleteById(id);
        if (rtn>0) {
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除成功", true);
        }else {
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除成功", false);
        }
    }

    @Override
    public NftResponse<Boolean> delCollectjk(Long id) {
        int rtn = collectjkMapper.deleteById(id);
        if (rtn>0) {
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除成功", true);
        }else {
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除成功", false);
        }
    }
}
